import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'
import Login from '../views/login'
import index from '../views/index'
Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/index',
      name: 'index',
      meta: {title:'首页'},//面包屑
      component: index,
      redirect: '/index/Welcome',//路由重定向
      // 添加路由
      children: [
        { path: '/index/Welcome', component: () => import('../views/Welcome') },
        { path: '/userlist', component: () => import('../views/userlist') },
        { path: '/userinfo', component: () => import('../views/PersonInfo') },
        { path: '/userauth', component: () => import('../views/auth')
        },
        { path: '/info/CloseAccount', component: () => import('../views/CloseAccount') },
        { path: '/info/information', component: () => import('../views/info') },
        { path: '/media/music', component: () => import('../views/music') }
      ]

    }
  ]
})
//路由导航守卫
// 挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，
// next是下一个要做的操作 next('/login')强制跳转login
router.beforeEach((to,form,next)=>{
    // console.log(to.path)
    // 访问登录页，放行
  if (to.path==='/login'){
    next()
  }
  else {
      // 获取token
    const token = window.sessionStorage.getItem('token');
      // 没有token, 强制跳转到登录页
    if(!token){
      next('/login')
    }
    else {
      next()
    }
  }
})
export default router
